<?php

class CICache {
	
	private static $cache_time = 4; // number of minutes
	
	public static function add($key, $value) {
//		Util::debug("Adding to cache ({$key}): {$value}");
		$redis = Database::connect();
		$user = new InsightUser();
		$cache_key = "cache:{$user->org_id}:{$user->user_id}:{$key}";
		$timeout = 60 * CICache::$cache_time;
		$redis->setex($cache_key, $timeout, $value);
	}
	
	public static function get($key) {
//		Util::debug("Searching cache: {$key}");
		$redis = Database::connect();
		$user = new InsightUser();
		$cache_key = "cache:{$user->org_id}:{$user->user_id}:{$key}";
		$value = $redis->get($cache_key);
		return $value; // returns FALSE if not present
	}
	
	public static function clear($key) {
//		Util::debug("Erasing cache for {$key}");
		$redis = Database::connect();
		$user = new InsightUser();
		$cache_key = "cache:{$user->org_id}:{$user->user_id}:{$key}";
		$redis->delete($cache_key);
	}
	
	// clears all cache entries for a user
	public static function clearAll() {
		$redis = Database::connect();
		$user = new InsightUser();
		$cache_key = "cache:{$user->org_id}:{$user->user_id}:*";
		
		foreach ($redis->keys($cache_key) as $key) {
			$redis->del($key);
		}
	}
	
}

?>